//
//  LMJHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LMJHomePageController.h"
#import "CK_MenuViewController.h"
#import "LMJNetWorkRequestManager.h"
#import "LMJCollectionViewCell.h"
#import "CycleView.h"
#import "LMJHomeDetailViewController.h"
#import "LMJRedioController.h"
#import "LMJLiangPinController.h"
#import "LMJTopicController.h"
#import "LMJRadioListController.h"
@interface LMJHomePageController ()<MenuClickDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
//UICollectionView属性
@property (strong,nonatomic) UICollectionView *collectionView;
//数组属性
@property (nonatomic, strong) NSMutableArray *dataSource;
//轮播图属性
@property (nonatomic, strong) CycleView *cycleView;
@end

@implementation LMJHomePageController

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    [super viewDidLoad];
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"今日 ·TODAY", @"电台 ·RADIO", @"音乐 ·MUSIC", @"良品 ·PRODUCTS", @"话题 ·TOPIC", @"设置 ·SETTING"] mutableCopy];
    
    //初始化数据源
    self.dataSource = [NSMutableArray new];
    [LMJNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:LMJ_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
//        NSLog(@"data====%@",data);
//        NSLog(@"response====%@",response);
//        NSLog(@"error====%@",error);
        //////////////解析的网址 @"http://api2.pianke.me/read/columns" // 阅读首页列
        //解析数据
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = jsonDict[@"data"];
//            NSLog(@"++++++%@",dataDic);
            for (NSDictionary *itemDict in dataDic[@"list"]) {
                LMJHomeModel *model = [LMJHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
            }
        }
        //回到主线程  (不能直接刷新,得回到主线程)
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新数据
            [self.collectionView reloadData];
        });
   }];
    
    //创建collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    //设置layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width *0.33, self.view.frame.size.height *0.24);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 228);
    layout.minimumInteritemSpacing = 1.5;
    layout.minimumLineSpacing = 1;
    
    
    //代理
    self.collectionView.dataSource =self;
    self.collectionView.delegate = self;
    //添加到父视图
    [self.view addSubview:self.collectionView];
    //注册cell
    [self .collectionView  registerNib:[UINib nibWithNibName:@"LMJCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    //注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    }
#pragma mark -UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //创建头部轮播图
        self.cycleView = [[CycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 228) interVal:3];
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        self.cycleView.imgNameArray = [@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg"] mutableCopy];
        /////点击时会触发的代码
        [self.cycleView addTapBlock:^(NSInteger index) {
            NSLog(@"当前点击第%ld页",(long)index); //点击时没反应是因为没有打开交互
        }];

        return header;
    }
    return nil;
}
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LMJHomeDetailViewController *detailVC = [[LMJHomeDetailViewController alloc]init];
    LMJHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark -UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LMJCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
//点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else {
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//
-(void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
    switch (index) {
        case 1:
//        {
//            LMJRedioController *redioVC = [[LMJRedioController alloc]init];
//            [self.navigationController pushViewController:redioVC animated:YES];
//        }
        {
            LMJRadioListController *radioVC = [[LMJRadioListController alloc]init];
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
        case 3:
        {
            LMJLiangPinController *liangPinVC = [[LMJLiangPinController alloc]init];
            [self.navigationController pushViewController:liangPinVC animated:YES];
        }
            break;
            case 4:
        {
            LMJTopicController *topicVC = [[LMJTopicController alloc]init];
            [self.navigationController pushViewController:topicVC animated:YES];
        }
            break;

        default:
            break;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
